package com.example.demo.service;

import com.example.demo.domain.UserDb;
import com.example.demo.enums.ResultEnum;
import com.example.demo.exception.UserException;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Administrator on 2017/7/6.
 */
@Service
public class UserService {

    @Autowired
    private UserRepository userRepository;

    @Transactional
    public void insertTwo(){
        UserDb userA=new UserDb();
        userA.setAge(2);
        userA.setName("am");
        userRepository.save(userA);

        UserDb userB=new UserDb();
        userB.setAge(3);
        userB.setName("Bm");
        userRepository.save(userB);
    }

    public void getAge(Integer id) throws Exception {
        UserDb userDb = userRepository.findOne(id);
        Integer age = userDb.getAge();
        if (age < 10) {
            throw new UserException(ResultEnum.PRIMARY_SCHOOL);
        } else if (age > 10 && age < 16) {
            throw new UserException(ResultEnum.MIDDLE_SCHOOL);
        }
    }

    public UserDb findOne(Integer id){
        return userRepository.findOne(id);
    }
}
